Find minimum value


Posted by Christy on 2022-04-21

Description: Write a function named findMin that accepts an array and find the minimum value

Note: Don’t use built-in function sort()

function findMin(arr) {
  let min = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (min > arr[i]) {
      min = arr[i];
    }
  }
  return min;
}

console.log(findMin([1, 2, 3])); // 1
console.log(findMin([1, 6, 0, 33, 44, 88, -10])); // -10

Assign a minimum value first, if this value is bigger than any other value in the array, renew the value.










Related Posts

[Day 03] 觀察者模式,命令模式,轉接器模式,表象模式

[Day 03] 觀察者模式,命令模式,轉接器模式,表象模式

GoogleChrome開發者工具教學 - Network

GoogleChrome開發者工具教學 - Network

《鳥哥 Linux 私房菜:基礎篇》Chapter 06 - Linux 的檔案與目錄管

《鳥哥 Linux 私房菜:基礎篇》Chapter 06 - Linux 的檔案與目錄管


Comments